//
//  OIDSystemSettingManager.m
//  EducationOID
//
//  Created by slz on 2017/6/27.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "OIDSystemSettingManager.h"

static NSString *const kCodingTipShake = @"tip_shake";
static NSString *const kCodingTipVoice = @"tip_voice";
static NSString *const kCodingCopyToBoader = @"copy_to_boader";
static NSString *const kCodingRemmenberDuplicate = @"recommend_duplicate";
static NSString *const kCodingSearchMoreInfo = @"search_more_info";
static NSString *const kCodingCameraLight = @"camera_light";
static NSString *const kCodingWordFilter = @"word_filter";
static NSString *const kCodingShakeToCamera = @"shake_camera";

@implementation OIDSystemSettingManager

+ (NSString *)cachePathOfSettingInfo
{
    //    return [[NSFileManager defaultManager] pathForPublicFile:@"user.data"];
    return [[NSFileManager defaultManager] pathForLibraryFile:@"systemset.data"];
}



+(OIDSystemSettingManager *)shareManager{
    static OIDSystemSettingManager * manager = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        manager = [OIDSystemSettingManager loadFromDisk];
        if (manager == nil)
        {
            manager = [[self alloc]init];
        }
    });
    return manager;
}

+(OIDSystemSettingManager *)loadFromDisk{
    NSString *cacheFilePath = [OIDSystemSettingManager cachePathOfSettingInfo];
    
    
    NSData* data = [NSData dataWithContentsOfFile:cacheFilePath];
    OIDSystemSettingManager* instance = nil;
    if (data)
    {
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        instance = [unarchiver decodeObject];
    }
    return instance;
}


#pragma mark - NSCoding methods
- (void)encodeWithCoder:(NSCoder *)aCoder;
{
    [aCoder encodeBool:_tipShake forKey:kCodingTipShake];
    [aCoder encodeBool:_tipVoice forKey:kCodingTipVoice];
    [aCoder encodeBool:_copyToBoarde forKey:kCodingCopyToBoader];
    [aCoder encodeBool:_remmenberDuplicate forKey:kCodingRemmenberDuplicate];
    [aCoder encodeBool:_searchMoreInfo forKey:kCodingSearchMoreInfo];
    [aCoder encodeBool:_cameraLight forKey:kCodingCameraLight];
    [aCoder encodeBool:_wordFilter forKey:kCodingWordFilter];
    [aCoder encodeBool:_shakeToCamera forKey:kCodingShakeToCamera];
}

- (id)initWithCoder:(NSCoder *)aDecoder;
{
    self = [super init];
    if (self)
    {
        _tipShake = [aDecoder decodeBoolForKey:kCodingTipShake];
        _tipVoice = [aDecoder decodeBoolForKey:kCodingTipVoice];
        _copyToBoarde = [aDecoder decodeBoolForKey:kCodingCopyToBoader];
        _remmenberDuplicate = [aDecoder decodeBoolForKey:kCodingRemmenberDuplicate];
        _searchMoreInfo = [aDecoder decodeBoolForKey:kCodingSearchMoreInfo];
        _cameraLight = [aDecoder decodeBoolForKey:kCodingCameraLight];
        _wordFilter = [aDecoder decodeBoolForKey:kCodingWordFilter];
        _shakeToCamera = [aDecoder decodeBoolForKey:kCodingShakeToCamera];
    }
    return self;
}

-(void)synchronizeToDisk{
    dispatch_async(dispatch_get_main_queue(), ^
        {
            
            NSMutableData * data = [NSMutableData data];
            NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
            [archiver encodeObject:self];
            [archiver finishEncoding];
            [data writeToFile:[OIDSystemSettingManager cachePathOfSettingInfo] atomically:YES];
            
        });
}

@end
